package org.example.com.Controller;
import com.alibaba.fastjson2.JSONArray;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.com.Bean.Alipay;
import org.example.com.Bean.Order;
import org.example.com.config.JwtConfig;
import org.example.com.service.OrderService;
import org.example.com.service.SeckillServcice;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;

@Controller
@RequestMapping("/seckill")
public class SeckilController {
    @Value("${appId}")
    private String appId;
    @Value("${privateKey}")
    private String privateKey;
    @Value("${publicKey}")
    private String publicKey;
    @Value("http://localhost:8004/seckill/success")
    private String notifyUrl;
    @Value("http://localhost:8004/seckill/success")
    private String returnUrl;
    @Value("${signType}")
    private String signType;
    @Value("${charset}")
    private String charset;
    @Value("${gatewayUrl}")
    private String gatewayUrl;
    @Resource
    private SeckillServcice seckillServcice;
    @GetMapping("/get_data")
    @ResponseBody
    public JsonResult getData(HttpServletRequest request) {
        this.token=request.getHeader("token");
        return RecultTool.success("success");

    }
    private String orderId;
    private String total_amount;
    private String token;
    private String[] ids = {""};
    @Resource
    private OrderService orderService;
    @GetMapping
    public String seckill() {
        JsonResult result = seckillServcice.seckill(this.token);
        System.out.println(result + ".....");
        String bookId = result.getData().toString();
        this.total_amount = result.getError();
        System.out.println(this.total_amount + "././");
        ids[0] = bookId;
        return "redirect:/seckill/pay";
    }

    @RequestMapping("/pay")
    @ResponseBody
    public String pay(Alipay alipayBean) throws AlipayApiException {
        alipayBean.setTotal_amount(this.total_amount);
        alipayBean.setSubject(this.ids[0].toString()+"的订单");
        alipayBean.setBody("秒杀的订单");
        this.orderId= new Date().getTime()+""+(int)(Math.random()*100000);
        alipayBean.setOut_trade_no(this.orderId);
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey,"json", charset,publicKey, signType);

        AlipayTradePagePayRequest alipayTradeAppPayRequest = new AlipayTradePagePayRequest();
        System.out.printf("进入了这里"+alipayBean);
        // 设置付款成功后应跳转的路径
        alipayTradeAppPayRequest.setNotifyUrl(notifyUrl);
        alipayTradeAppPayRequest.setReturnUrl(returnUrl);
        // 设置支付宝的各项参数
        String json = JSONArray.toJSONString(alipayBean);
        alipayTradeAppPayRequest.setBizContent(json);
        System.out.println("支付的参数是：" + json);
        System.out.println(JSONArray.toJSONString(alipayTradeAppPayRequest));
        // 生成最终的订单
        return alipayClient.pageExecute(alipayTradeAppPayRequest).getBody();
    }
    @GetMapping("/success")
    public String success(){
        Order order=new Order();
        System.out.println(token+"....");
        String userId= JwtConfig.parseJWT(token).get("id").toString();
        order.setId(orderId);
        order.setUserId(userId);
        order.setTotalAmount(total_amount);
        order.setTotalCount("1");

        orderService.saveOrder(order,ids);

        return "redirect:http://localhost:8082/front/index.html";




    }


}
